package hello.java.app2;

import java.util.Random;
import java.util.concurrent.CompletableFuture;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WsWrapper {
    private static final Logger logger = LoggerFactory.getLogger(WsWrapper.class);
    private static final Random random = new Random();
    private static WsClient wsClient;

    public static void run(String url, String username, String pwd, boolean randomDelay) {
        CompletableFuture.runAsync(() -> {
            try {
                if (randomDelay) {
                    // 支持随机时间重连
                    Thread.sleep((random.nextInt(6) + 1) * 1000);
                }
                logger.info("url:" + url + " username:" + username + " pwd:" + pwd);
                wsClient = new WsClient(url, username, pwd);
                wsClient.connect();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
    }

    public static void stop() {
        if (wsClient != null) {
            wsClient.disconnect();
        }
    }
}
